﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SmartSpace.WebApp
{
    public class SspModule : IHttpModule
    {
        private HttpApplication _context;

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            _context = context;

            context.BeginRequest += context_BeginRequest;
        }

        void context_BeginRequest(object sender, EventArgs e)
        {
            string path = _context.Request.Url.AbsolutePath.ToLower();
            if (path.EndsWith(".ssp"))
            {
                System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("(?<page>[^\\/]*).ssp");
                System.Text.RegularExpressions.Match match = regex.Match(path);
                string page = match.Groups["page"].Value;

                _context.Server.Transfer(string.Format("./_dynamic.aspx?__page={0}&{1}", page, _context.Request.QueryString.ToString()));
            }
        }
    }
}